 IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sprSelect_NamesWithMatchParent')
	BEGIN
		DROP  Procedure  sprSelect_NamesWithMatchParent
	END

GO

CREATE Procedure sprSelect_NamesWithMatchParent
	@providerNamePk int,
	@threshold int
AS

	declare @parentFk uniqueidentifier
	
	select @parentFk = dbo.fnGetProviderNameParentMatch( @providerNamePk )
	
	if (@parentFk is not null)
	begin
		if ((select count(*) from tmpMatchResults) = 0) --assume this is the first match
		begin
			insert tmpMatchResults
			select n.NameGuid, 100
			from tblName n
			where n.NameParentFk = @parentFk
		end
		else
		begin
			delete mr
			from tmpMatchResults mr
			inner join tblName n on n.NameGuid = mr.MatchResultRecordId
			where n.NameParentFk is null or n.NameParentFk <> @parentFk
		end
	end
	else
	begin		
		delete tmpMatchResults
	end
		
	
GO


GRANT EXEC ON sprSelect_NamesWithMatchParent TO PUBLIC

GO


